<?php

/**
 * @author Administrator
 *
 */
abstract class Worker{
	
	protected $log;
	protected $config  = null;
	protected $runData = null;
	protected $module  = null;
	protected $worker   = null;
	protected $action  = null;
	protected $connection = null;
	
	public function __construct($param = null,$connection = null){		
		$this->config = new Config();
		if(isset($param['module']))
			$this->module = $param['module'];
		if(isset($param['worker']))
			$this->worker = $param['worker'];
		if(isset($param['action']))
			$this->action = $param['action'];		
		$this->connection = $connection;
		$this->log = new Log();		
	}
	
	protected function socketWrite($data){
		if(is_array($data)){
			$data['worker'] = $this->worker;
			$data['action'] = $this->action;			
			$data = json_encode($data,JSON_UNESCAPED_UNICODE | JSON_NUMERIC_CHECK);
		}
		if(IS_CLI)
			socket_write($this->connection, $data."\n");
		else 
			print_r(json_decode($data,true));		
	}
	//abstract public function work($runData);
	/**
	 * 
	 * @param unknown_type $psModelName
	 * @return Abstracts
	 */
	protected function loadModel($psModelName){
		include_once "model/".$psModelName . 'Model.php';
		$modelClassName = $psModelName."Model";
		if(class_exists($modelClassName)){			
			return new $modelClassName();
		}
	}
	
	public function preRun($runData){
		$this->setRunData($runData);
	}
	/**
	 * @return the $log
	 */
	public function getLog() {
		return $this->log;
	}

	/**
	 * @param field_type $log
	 */
	public function setLog($log) {
		$this->log = $log;
	}

	/**
	 * @return the $runData
	 */
	public function getRunData() {
		return $this->runData;
	}

	/**
	 * @param multitype: $runData
	 */
	public function setRunData($runData) {			
		$this->runData = $runData;
	}

	
}